<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div>
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-circle-plus-outline"></i>增加
          </el-button>
          <el-button type="danger" @click="handleDeleteAll">
            <i class="el-icon-delete"></i>删除
          </el-button>
        </div>
        <div class="flex mt-3">
          <el-select
            v-model="listQuery.op_type"
            placeholder="操作类型"
            class="mr-4"
            style="width: 17%"
          >
            <el-option
              v-for="item in cate_options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-input
            v-model="listQuery.gift_name"
            class="mr-4"
            style="width: 17%"
            placeholder="请输入礼物名称"
          ></el-input>
          <el-select
            v-model="listQuery.sale_status"
            placeholder="开启状态"
            style="width: 17%"
          >
            <el-option
              v-for="item in sale_options"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <div class="ml-4 flex items-center" style="width: 17%">
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button type="primary" @click="handleReset">重置</el-button>
          </div>
        </div>
      </div>
      <el-table
        :data="menuList"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="礼物名称" prop="name"></el-table-column>
        <el-table-column label="礼物价格" prop="price"></el-table-column>
        <el-table-column label="礼物图片" prop="picture">
          <template slot-scope="scope">
            <el-popover placement="top" max-width="420" trigger="hover">
              <img
                :src="scope.row.picture"
                alt=""
                style="max-width: 200px; max-height: 200px"
              />
              <img
                slot="reference"
                :src="scope.row.picture"
                alt=""
                style="width: 50px"
              />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="礼物动效">
          <template slot-scope="{ row }">
            <div
              v-for="(item, index) in row.special_list"
              :key="'动效' + index"
            >
              <el-popover placement="top" max-width="420" trigger="hover">
                <img
                  :src="item"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img slot="reference" :src="item" alt="" style="width: 50px" />
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作类型" prop="op_type_name"></el-table-column>
        <el-table-column label="分类" prop="category_name"></el-table-column>
        <el-table-column label="状态" prop="sold">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sold" type="primary">上架</el-tag>
            <el-tag v-else type="danger">下架</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="心动值" prop="cardiac"></el-table-column>
        <el-table-column label="礼物说明" prop="desc"></el-table-column>
        <el-table-column label="礼物标签" prop="">
          <template slot-scope="{ row }">
            <div v-for="item in row.tags" :key="item.id">
              <p>{{ item.name }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="链接" prop="url"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column
          label="概率"
          prop="success_probability"
        ></el-table-column>
        <el-table-column label="震动效果" prop="is_shock">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.is_shock">有</el-tag>
            <el-tag type="warning" v-else>无</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="时长" prop="magic_time"></el-table-column>
        <el-table-column label="震动延时" prop="delay_shock"></el-table-column>
        <el-table-column label="是否展示在礼物墙">
          <template slot-scope="{ row }">
            <el-tag v-if="row.is_show_in_wall === 1" type="primary">是</el-tag>
            <el-tag v-else type="info">否</el-tag>
          </template>
        </el-table-column>
        =
        <el-table-column fixed="right" label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              plain
              @click="handleEdit(scope.row)"
            >
              <i class="el-icon-edit-outline"></i>编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              plain
              @click="handleSend(scope.row)"
            >
              <i class="el-icon-present"></i>赠送
            </el-button>

            <el-dropdown class="ml-3">
              <el-button type="primary">
                更多操作<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="handleChangeStatus(scope.row)">
                  {{ scope.row.sold ? "下架" : "上架" }}
                </el-dropdown-item>
                <el-dropdown-item
                  @click.native="handleStatusCrontab(scope.row)"
                >
                  定时任务
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleSetTop(scope.row)">
                  置顶
                </el-dropdown-item>
                <el-dropdown-item @click.native="handleDeleteItem(scope.row)">
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <cus-pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.page_size"
      @pagination="handleGetData"
    />

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style: normal; color: red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleDelete">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 设置定时上下架 -->
    <el-dialog
      title="这是定时上下架?"
      :visible.sync="statusCrontabVisible"
      width="435px"
      :before-close="handleBeforeCloseStatusCrontab"
    >
      <el-form
        ref="statusCrontabForm"
        :model="statusCrontabForm"
        label-width="120"
        :rules="statusCrontabRules"
      >
        <el-form-item label="操作" prop="action">
          <el-radio v-model="statusCrontabForm.action" :label="1">
            上架
          </el-radio>
          <el-radio v-model="statusCrontabForm.action" :label="0">
            下架
          </el-radio>
        </el-form-item>
        <el-form-item label="设置时间" prop="set_time">
          <el-date-picker
            v-model="statusCrontabForm.set_time"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="statusCrontabVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handleConfirmStatusCrontab('statusCrontabForm')"
        >
          确 定
        </el-button>
      </span>
    </el-dialog>

    <addEditModal
      :dialogVisible.sync="dialogVisible"
      :title.sync="dialogTitle"
      :id="rowData.id ? rowData.id : -1"
      @updateTable="handleSearch"
    ></addEditModal>

    <sendModal
      :dialogVisible.sync="sendVisible"
      :id="sendRowData.id ? sendRowData.id : -1"
    ></sendModal>
  </div>
</template>

<script>
import {
  getData,
  deleteData,
  getTypeData,
  postStatusCrontab,
  postSetTop,
  postChangeStatus
} from "@/api/roomGift/magicGift";

import addEditModal from "./childComps/addEditModal";
import sendModal from "./childComps/sendModal.vue";

export default {
  name: "",
  components: {
    addEditModal,
    sendModal
  },
  data() {
    return {
      sale_options: [
        {
          id: -1,
          title: "全部状态"
        },
        {
          id: 0,
          title: "下架"
        },
        {
          id: 1,
          title: "上架"
        }
      ],
      cate_options: [],
      listQuery: {
        op_type: undefined,
        gift_name: undefined,
        sale_status: -1,
        page: 1,
        page_size: 10
      },
      menuList: [],
      total: 0,
      multipleSelection: [],
      dialogTitle: "add",

      rowData: {},
      delete_ids: undefined,
      dialogVisible: false,

      deleteDialogVisible: false,

      sendVisible: false,
      sendRowData: {},

      /** 设置定时上下架 */
      statusCrontabVisible: false,
      statusCrontabForm: {
        id: undefined,
        action: undefined,
        set_time: undefined
      },
      statusCrontabRules: {
        action: [{ required: true, message: "请选择操作", trigger: "change" }],
        set_time: [
          { required: true, message: "请选择定时时间", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.handleGetData();
    this.handleGetTypeData();
  },
  methods: {
    /**获取首页数据 */
    handleGetData() {
      getData(this.listQuery).then(res => {
        this.menuList = res.data.data;
        this.total = res.data.total;
      });
    },
    /**获取操作类型 */
    handleGetTypeData() {
      getTypeData().then(res => {
        this.cate_options = res.data;
      });
    },
    /**搜索 */
    handleSearch() {
      this.listQuery.page = 1;
      this.handleGetData();
    },
    /**重置 */
    handleReset() {
      this.listQuery = {
        op_type: undefined,
        gift_name: undefined,
        sale_status: -1,
        page: 1,
        page_size: 10
      };
      this.handleGetData();
    },
    /**选择单选框 */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /**添加 */
    handleAdd() {
      this.dialogTitle = "add";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.rowData = row;
      this.dialogTitle = "edit";
      this.dialogVisible = true;
    },
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请勾选要删除的数据");
        return;
      } else {
        let arr = [];
        this.multipleSelection.forEach(item => {
          arr.push(item.id);
        });
        this.delete_ids = arr.join(",");
      }
      this.deleteDialogVisible = true;
    },
    handleDeleteItem(row) {
      this.delete_ids = row.id.toString();
      this.deleteDialogVisible = true;
    },
    handleDelete() {
      deleteData(this.delete_ids).then(res => {
        if (res.code === 200) {
          this.handleGetData();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      });
    },
    handleSend(row) {
      this.sendRowData = row;
      this.sendVisible = true;
    },

    /**设置定时上下架 */
    handleStatusCrontab(row) {
      this.statusCrontabVisible = true;
      this.statusCrontabForm.id = row.id;
    },
    handleConfirmStatusCrontab(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          postStatusCrontab(this.statusCrontabForm).then(res => {
            if (res.code === 200) {
              this.handleReset();
              this.statusCrontabVisible = false;
              this.$notify({
                type: "success",
                message: "操作成功",
                position: "bottom-right",
                showClose: false,
                duration: 3000
              });
            }
          });
        } else {

          return false;
        }
      });
    },
    handleBeforeCloseStatusCrontab() {
      this.statusCrontabForm = {
        id: undefined,
        action: undefined,
        set_time: undefined
      };
    },

    /**置顶 */
    handleSetTop(row) {
      postSetTop(row.id).then(res => {
        if (res.code === 200) {
          this.handleGetData();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      });
    },
    /**切换上下架 */
    handleChangeStatus(row) {
      postChangeStatus(row.id).then(res => {
        if (res.code === 200) {
          this.handleGetData();
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
